package GUI;

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * The functionPanel class
 * 
 * @author Abraham Santini
 *
 */
public class FunctionPanel extends JPanel{
	/**
	 * A Panel to hold the components of the FunctionPanel
	 */
	private JPanel my_panel;
	/**
	 * A JComboBox to allow the user to select a function.
	 */
	private JComboBox my_box;
	/**
	 * A JLabel that will display the results of a function.
	 */
	private JLabel my_label;
	/**
	 * Holds the JTextFields that the user selects.
	 */
	private ArrayList<JTextField> my_fields = new ArrayList<JTextField>();
	/**
	 * Public constructor for creating a FunctionPanel
	 */
	public FunctionPanel () {
		my_panel = new JPanel();
		my_panel.setVisible(true);
		my_panel.setLayout(new BoxLayout(my_panel, BoxLayout.X_AXIS));
		
		my_box = new JComboBox();
		my_box.addItem("SUM");
		my_box.addItem("MIN");
		my_box.addItem("MAX");
		my_box.addItem("AVE");
		my_panel.add(my_box);
		
		my_label = new JLabel("Function result");
		my_panel.add(my_label);
	}
	/**
	 * Returns the FunctionPanels JLabel.
	 * @return my_label
	 */
	public JLabel getLabel() {
		return my_label;
	}
	/**
	 * Gets the JPanel that contains the FunctionPanel.
	 * @return my_panel The JPanel that contains the FunctionPanel.
	 */
	public JPanel getPanel() {
		return my_panel;
	}
	/**
	 * The ArrayList of selected JTextFields
	 * @return my_fields That List of JTextFields that have been selected.
	 */
	public ArrayList<JTextField> getFields() {
		return my_fields;
	}
	/**
	 * Gets the JComboBox that contains the different functions the user can select.
	 * @return my_box The JCombBox that holds the different function options. 
	 */
	public JComboBox getBox() {
		return my_box;
	}
	/**
	 * Performs the selected function on the selected JTExtFields.
	 */
	public void evaluateFunction() {
		if(my_box.getSelectedItem() == "SUM") {
			double temp = 0;
			for( JTextField the_field : my_fields) {
				temp += Double.parseDouble(the_field.getText());
			}
			my_label.setText(Double.toString(temp));
		} else if (my_box.getSelectedItem() == "MIN") {
			double temp = Double.POSITIVE_INFINITY;
			for( JTextField the_field : my_fields) {
				if(Double.parseDouble(the_field.getText()) < temp)
				temp = Double.parseDouble(the_field.getText());
			}
			my_label.setText(Double.toString(temp));
		} else if (my_box.getSelectedItem() == "MAX") {
			double temp = Double.NEGATIVE_INFINITY;
			for( JTextField the_field : my_fields) {
				if(Double.parseDouble(the_field.getText()) > temp)
				temp = Double.parseDouble(the_field.getText());
			}
			my_label.setText(Double.toString(temp));
		} else {
			double temp = 0;
			for( JTextField the_field : my_fields) {
				temp += Double.parseDouble(the_field.getText());
			}
			temp = temp/my_fields.size();
			my_label.setText(Double.toString(temp));
		}	
	}
	/**
	 * Resets the ArrayList of JTextFields and resets the JTextFields background color to white.
	 */
	public void killFunction() {
		if(!my_fields.isEmpty()){
			for( JTextField the_field : my_fields) {
				the_field.setBackground(Color.WHITE);
			}
			my_fields.clear();
			my_label.setText("Function result");
		}
	}
}
